package com.team.engine.user.service;

import java.rmi.server.UID;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.team.engine.common.exception.BaseServiceException;
import com.team.engine.user.dao.UserNavDaoImpl;
import com.team.engine.user.dao.UserGroup;
import com.team.engine.user.dao.UserGroupDaoImpl;
import com.team.engine.user.dao.UserGroupUser;
import com.team.engine.user.dao.UserGroupUserDaoImpl;
import com.team.engine.user.dao.UserNav;
import com.team.engine.util.QuickDao;
import com.team.engine.util.SubPage;

@Service
@Scope("prototype")
public class UserServiceImpl {

	@Resource
	private QuickDao<?, ?> quickDao;
	
	@Resource
	private UserNavDaoImpl userNavDaoImpl;
	
	@Resource
	private UserGroupDaoImpl userGroupDaoImpl;
	
	@Resource
	private UserGroupUserDaoImpl userGroupUserDaoImpl;

	/**
	 * 添加一个用户
	 * @param userNav
	 * @return
	 * @throws BaseServiceException
	 */
	public boolean addUser(UserNav userNav) throws BaseServiceException {
		String n = null;
		try {
			QuickDao<UserNav, String> dao = (QuickDao<UserNav, String>) this.quickDao;
			QuickDao<UserNav, String> instance = dao.getInstance(UserNav.class);
			String uid = new UID().toString();
			userNav.setUserId(uid);
			userNav.getUserDetails().setUserId(uid);
			n = instance.insert(userNav);

		} catch (RuntimeException e) {
			throw new BaseServiceException(e);
		}

		return n == null ? false : true;
	}
	
	/**
	 * 根据用户名查询用户信息
	 * @param name
	 * @return
	 */
	public List<UserNav> getUserByName(UserNav userNav){
		List<UserNav> selectByName = this.userNavDaoImpl.selectByName(userNav.getName());
		return selectByName;
	}
	
	/**
	 * 根据用户组名查询用户组信息
	 * @param name
	 * @return
	 */
	public List<UserGroup> getUserGroupByName(UserGroup userGroup){
		List<UserGroup> selectByName = this.userGroupDaoImpl.selectByName(userGroup.getNameNav());
		return selectByName;
	}
	
	/**
	 * 验证用户名是否重复，重复为true
	 * @param userNav
	 * @return
	 */
	public boolean isUserRepeat(UserNav userNav){
		return !this.getUserByName(userNav).isEmpty();
	}
	
	/**
	 * 取得所有用户
	 * @param page 翻页页码
	 * @return
	 */
	public SubPage<UserNav> getUserAll(String page){
		return this.userNavDaoImpl.selectAll(page==null||page.trim().length()==0?0:Integer.parseInt(page));
	}
	
	/**
	 * 取得所有用户，及其所在的用户组
	 * @param page 翻页页码
	 * @return
	 */
	public SubPage<UserNav> getUserAllWithGroup(String page, String rowsCount){
		return this.userNavDaoImpl.selectAllWithGroup(page, rowsCount);
	}
	
	/**
	 * 验证用户组名是否重复，重复为true
	 * @param userGroup
	 * @return
	 */
	public boolean isUsergroupRepeat(UserGroup userGroup){
		return !this.getUserGroupByName(userGroup).isEmpty();
	}
	
	/**
	 * 添加一个用户组
	 * @param userGroup
	 * @return
	 * @throws BaseServiceException
	 */
	public boolean addUserGroup(UserGroup userGroup) throws BaseServiceException {
		Integer n = null;
		try {
			QuickDao<UserGroup, Integer> dao = (QuickDao<UserGroup, Integer>) this.quickDao;
			QuickDao<UserGroup, Integer> instance = dao.getInstance(UserGroup.class);
			n = instance.insert(userGroup);

		} catch (RuntimeException e) {
			throw new BaseServiceException(e);
		}

		return n == null ? false : true;
	}
	
	/**
	 * 添加用户组和一个用户，或一批用户的关联
	 * @param userGroup 用户组实例
	 * @param userNavs 用户实例
	 * @return
	 */
	public boolean addTUsergroupUser(UserGroup tUsergroup, UserNav...userNavs){
		if (userNavs.length > 1) {
			UserGroupUser tUsergroupUser = new UserGroupUser();
			tUsergroupUser.setUsergroupId(tUsergroup.getUsergroupId());
			tUsergroupUser.setUserId(userNavs[0].getUserId());
			this.userGroupUserDaoImpl.insert(tUsergroupUser);
		} else if(userNavs.length == 1) {
			ArrayList<UserGroupUser> list = new ArrayList<UserGroupUser>();
			
			for(UserNav tUser: userNavs){
				UserGroupUser tUsergroupUser = new UserGroupUser();
				tUsergroupUser.setUsergroupId(tUsergroup.getUsergroupId());
				tUsergroupUser.setUserId(userNavs[0].getUserId());
				list.add(tUsergroupUser);
			}
			this.userGroupUserDaoImpl.insertBatch(list);
		}
		
		return true;
	}
	
	/**
	 * 取得所有用户组
	 * @param page 翻页页码
	 * @return
	 */
	public SubPage<UserGroup> getUsergroupAll(String page){
		return this.userGroupDaoImpl.selectAll(page==null||page.trim().length()==0?0:Integer.parseInt(page));
	}
	
	public void setQuickDao(QuickDao<?, ?> quickDao) {
		this.quickDao = quickDao;
	}
	
	public void setUserNavDaoImpl(UserNavDaoImpl userNavDaoImpl) {
		this.userNavDaoImpl = userNavDaoImpl;
	}

	public void setUserGroupDaoImpl(UserGroupDaoImpl userGroupDaoImpl) {
		this.userGroupDaoImpl = userGroupDaoImpl;
	}

	public void setUserGroupUserDaoImpl(UserGroupUserDaoImpl userGroupUserDaoImpl) {
		this.userGroupUserDaoImpl = userGroupUserDaoImpl;
	}
}
